package cn.wuxing.mediation.web.controller.statistics;

import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.core.page.PageDomain;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.core.page.TableSupport;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import cn.wuxing.mediation.system.domain.vo.*;
import cn.wuxing.mediation.system.service.ISAnalysisService;
import cn.wuxing.mediation.system.service.StatisticsService;
import cn.wuxing.mediation.system.service.impl.StatisticsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;


/**
 * 统计分析列表穿透
 */
@Slf4j
@RestController
@RequestMapping("/analysis")
public class AnalysisController extends BaseController {
    @Resource
    private ISAnalysisService analysisService;

    @Resource
    private StatisticsService statisticsService;

    /**
     * 1、 纠纷案件总况分析-案件数量
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/disputeCount/area")
    public TableDataInfo getDisputeCount(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<AnalysisVo> list = analysisService.getDisputeCount(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 纠纷案件总况导出
     *
     * @param response
     * @param analysisQueryVo
     */
    @PostMapping("/disputeCount/export")
    public void disputeCountExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        List<Object> dateResult = statisticsService
                .overallSituation(analysisQueryVo.getSource()
                        , analysisQueryVo.getEventType()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());
        List<DisputeCasesVo> list = new ArrayList<>();
        if (dateResult.size() > 0) {
            dateResult.forEach(data -> {
                DisputeCasesVo disputeCasesVo = new DisputeCasesVo();
                Map<String, Object> regionData = (Map<String, Object>) data;
                disputeCasesVo.setAreaName((String) regionData.get("addr"));//区域
                List<Map<String, Object>> dataCounts = (List<Map<String, Object>>) regionData.get("data");

                disputeCasesVo.setDisputeCount(String.valueOf(dataCounts.get(0).get("num")));//案件总数
                disputeCasesVo.setToBeAssignedCount(String.valueOf(dataCounts.get(1).get("num")));//待分派
                disputeCasesVo.setNumberOfAssignmentsCount(String.valueOf(dataCounts.get(2).get("num")));//已分派
                disputeCasesVo.setPendingAcceptancesCount(String.valueOf(dataCounts.get(3).get("num")));//待受理数
                disputeCasesVo.setNumberOfAcceptancesCount(String.valueOf(dataCounts.get(4).get("num")));//已受理数
                disputeCasesVo.setNumberReconciledCount(String.valueOf(dataCounts.get(5).get("num")));//待调解数
                disputeCasesVo.setNumberMediationCount(String.valueOf(dataCounts.get(6).get("num")));//已调解数
                disputeCasesVo.setMediationSuccessCount(String.valueOf(dataCounts.get(7).get("num")));//调解成功数
                disputeCasesVo.setSuccessRateCount(String.valueOf(dataCounts.get(7).get("num")) + "%");//调解成功率
                list.add(disputeCasesVo);
            });
        }
        ExcelUtil<DisputeCasesVo> excelUtil = new ExcelUtil<DisputeCasesVo>(DisputeCasesVo.class);
        excelUtil.exportExcel(response, list, "纠纷案件总况");
    }

    /**
     * 2、纠纷类型分析-案件数量
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/disputeType/type")
    public TableDataInfo getDisputeType(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<AnalysisTypeVo> list = analysisService.getDisputeCountByType(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 纠纷案件类型导出
     *
     * @param response
     * @param analysisQueryVo
     */
    @PostMapping("/disputeType/export")
    public void disputeTypeExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        List<Object> dateResult = statisticsService
                .typeStatistics(analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());
        List<DisputeTypeVo> list = new ArrayList<>();
        if (dateResult.size() > 0) {
            dateResult.forEach(data -> {
                DisputeTypeVo disputeCasesVo = new DisputeTypeVo();
                Map<String, Object> regionData = (Map<String, Object>) data;
                disputeCasesVo.setDisputeName((String) regionData.get("addr"));//区域
                List<Map<String, Object>> dataCounts = (List<Map<String, Object>>) regionData.get("data");

                disputeCasesVo.setDisputeCount(String.valueOf(dataCounts.get(0).get("num")));//案件总数
                disputeCasesVo.setToBeAssignedCount(String.valueOf(dataCounts.get(1).get("num")));//待分派
                disputeCasesVo.setNumberOfAssignmentsCount(String.valueOf(dataCounts.get(2).get("num")));//已分派
                disputeCasesVo.setPendingAcceptancesCount(String.valueOf(dataCounts.get(3).get("num")));//待受理数
                disputeCasesVo.setNumberOfAcceptancesCount(String.valueOf(dataCounts.get(4).get("num")));//已受理数
                disputeCasesVo.setNumberReconciledCount(String.valueOf(dataCounts.get(5).get("num")));//待调解数
                disputeCasesVo.setNumberMediationCount(String.valueOf(dataCounts.get(6).get("num")));//已调解数
                disputeCasesVo.setMediationSuccessCount(String.valueOf(dataCounts.get(7).get("num")));//调解成功数
                disputeCasesVo.setSuccessRateCount(String.valueOf(dataCounts.get(7).get("num")) + "%");//调解成功率
                list.add(disputeCasesVo);
            });
        }
        ExcelUtil<DisputeTypeVo> excelUtil = new ExcelUtil<DisputeTypeVo>(DisputeTypeVo.class);
        excelUtil.exportExcel(response, list, "纠纷类型分析");
    }

    /**
     * 3、纠纷区域分析-纠纷类型
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/disputeArea/type")
    public TableDataInfo disputesAnalysis(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeAnalysisVo> list = analysisService.disputesAnalysis(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 纠纷类型区域导出
     *
     * @param response
     * @param analysisQueryVo
     */
    @PostMapping("/disputeArea/export")
    public void disputeTypeAreaExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("纠纷类型区域分析");

        //获取数据
        List<Object> dateResult = statisticsService
                .regionalAnalysis(analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());

        // 动态设置表头
        List<String> headers = new ArrayList<>();
        Map<String, Object> head = (Map<String, Object>) dateResult.get(0);
        headers.add("区域");
        List<Map<String, Object>> data = (List<Map<String, Object>>) head.get("data");
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> map = data.get(i);
            headers.add((String) map.get("type"));
        }

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 填充表头
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        //设置数据
        for (int i = 0; i < dateResult.size(); i++) {
            // 这里可以添加数据行
            Row dataRow = sheet.createRow(i + 1);
            Map<String, Object> maps = (Map<String, Object>) dateResult.get(i);
            dataRow.createCell(0).setCellValue((String) maps.get("addr"));
            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) maps.get("data");
            for (int j = 0; j < dataAll.size(); j++) {
                Map<String, Object> map = dataAll.get(j);
                dataRow.createCell(j + 1).setCellValue(String.valueOf(map.get("num")));
            }
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("纠纷类型区域.xlsx")) {
            workbook.write(fileOut);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
            log.info("success!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                log.info("error!");
                e.printStackTrace();
            }
        }
    }

    /**
     * 4、纠纷主体分析
     */
    @GetMapping("/disputeSubject/subject")
    public AjaxResult disputeSubject(AnalysisQueryVo analysisQueryVo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        analysisQueryVo.setCurrentPage((pageNum-1)*pageSize);
        analysisQueryVo.setPageSize(pageSize);
        return analysisService.disputeSubject(analysisQueryVo);
    }

    /**
     * 4、纠纷主体导出
     */
    @PostMapping("/disputeSubject/export")
    public void disputeSubjectExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        List<Object> dateResult = statisticsService
                .subjectStatistics(analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());
        List<DisputeSubjectVo> list = new ArrayList<>();
        if (dateResult.size() > 0) {
            dateResult.forEach(data -> {
                DisputeSubjectVo disputeSubjectVo = new DisputeSubjectVo();
                Map<String, Object> regionData = (Map<String, Object>) data;
                disputeSubjectVo.setArea((String) regionData.get("addr"));//区域
                List<Map<String, Object>> dataCounts = (List<Map<String, Object>>) regionData.get("data");

                disputeSubjectVo.setCount(String.valueOf(dataCounts.get(0).get("num")));//案件总数
                disputeSubjectVo.setNaturalPerson(String.valueOf(dataCounts.get(1).get("num")));//待分派
                disputeSubjectVo.setLegalEntities(String.valueOf(dataCounts.get(2).get("num")));//已分派
                disputeSubjectVo.setUnOrganizations(String.valueOf(dataCounts.get(3).get("num")));//待受理数
                list.add(disputeSubjectVo);
            });
        }
        ExcelUtil<DisputeSubjectVo> excelUtil = new ExcelUtil<DisputeSubjectVo>(DisputeSubjectVo.class);
        excelUtil.exportExcel(response, list, "纠纷主体分析");
    }

    /**
     * 5、纠纷趋势分析
     */
    @GetMapping("/disputeTrend/trend")
    public TableDataInfo disputeTrend(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeTrendVo> list = analysisService.disputeTrend(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 5、纠纷趋势导出
     */
    @PostMapping("/disputeTrend/export")
    public void disputeTrendExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("纠纷趋势分析");

        //获取数据
        List<Object> dateResult = statisticsService
                .trendStatistics(analysisQueryVo.getDayNum()
                        ,analysisQueryVo.getSource()
                        ,analysisQueryVo.getEventType()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());

        // 动态设置表头
        List<String> headers = new ArrayList<>();
        Map<String, Object> head = (Map<String, Object>) dateResult.get(0);
        headers.add("区域");
        List<Map<String, Object>> data = (List<Map<String, Object>>) head.get("data");
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> map = data.get(i);
            headers.add((String) map.get("type"));
        }

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 填充表头
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        //设置数据
        for (int i = 0; i < dateResult.size(); i++) {
            // 这里可以添加数据行
            Row dataRow = sheet.createRow(i + 1);
            Map<String, Object> maps = (Map<String, Object>) dateResult.get(i);
            dataRow.createCell(0).setCellValue((String) maps.get("addr"));
            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) maps.get("data");
            for (int j = 0; j < dataAll.size(); j++) {
                Map<String, Object> map = dataAll.get(j);
                dataRow.createCell(j + 1).setCellValue(String.valueOf(map.get("num")));
            }
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("纠纷趋势分析.xlsx")) {
            workbook.write(fileOut);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
            log.info("success!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                log.info("error!");
                e.printStackTrace();
            }
        }
    }

    /**
     * 6、涉事人员年龄
     */
    @GetMapping("/disputeAge/age")
    public AjaxResult disputeAge(AnalysisQueryVo analysisQueryVo) {
        PageDomain pageDomain = TableSupport.getPageDomain();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        analysisQueryVo.setCurrentPage((pageNum-1)*pageSize);
        analysisQueryVo.setPageSize(pageSize);
        return analysisService.disputeAge(analysisQueryVo);
    }

    /**
     * 6、涉事人员年龄导出
     */
    @PostMapping("/disputeAge/export")
    public void disputeAgeExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        List<Object> dateResult = statisticsService
                .ageStatistics(analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());
        List<DisputeAgeVo> list = new ArrayList<>();
        if (dateResult.size() > 0) {
            dateResult.forEach(data -> {
                DisputeAgeVo disputeSubjectVo = new DisputeAgeVo();
                Map<String, Object> regionData = (Map<String, Object>) data;
                disputeSubjectVo.setArea((String) regionData.get("addr"));//区域
                List<Map<String, Object>> dataCounts = (List<Map<String, Object>>) regionData.get("data");

                disputeSubjectVo.setCount(String.valueOf(dataCounts.get(0).get("num")));//案件总数
                disputeSubjectVo.setCount18(String.valueOf(dataCounts.get(1).get("num")));//18
                disputeSubjectVo.setCount19_29(String.valueOf(dataCounts.get(2).get("num")));//19-29
                disputeSubjectVo.setCount30_39(String.valueOf(dataCounts.get(3).get("num")));//30-39
                disputeSubjectVo.setCount40_59(String.valueOf(dataCounts.get(4).get("num")));//40-59
                disputeSubjectVo.setCount60_79(String.valueOf(dataCounts.get(5).get("num")));//60-79
                disputeSubjectVo.setCount80(String.valueOf(dataCounts.get(6).get("num")));//80
                list.add(disputeSubjectVo);
            });
        }
        ExcelUtil<DisputeAgeVo> excelUtil = new ExcelUtil<DisputeAgeVo>(DisputeAgeVo.class);
        excelUtil.exportExcel(response, list, "涉事人员年龄分析");
    }

    /**
     * 7、涉事人员性别
     */
    @GetMapping("/disputeSex/sex")
    public AjaxResult disputeSex(AnalysisQueryVo analysisQueryVo) {
        PageDomain pageDomain = TableSupport.getPageDomain();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        analysisQueryVo.setCurrentPage((pageNum-1)*pageSize);
        analysisQueryVo.setPageSize(pageSize);
        return analysisService.disputeSex(analysisQueryVo);
    }

    /**
     * 7、涉事人员性别导出
     */
    @PostMapping("/disputeSex/export")
    public void disputeSexExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        List<Object> dateResult = statisticsService
                .sexStatistics(analysisQueryVo.getSource()
                        ,analysisQueryVo.getEventType()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());
        List<DisputeSexVo> list = new ArrayList<>();
        if (dateResult.size() > 0) {
            dateResult.forEach(data -> {
                DisputeSexVo disputeSubjectVo = new DisputeSexVo();
                Map<String, Object> regionData = (Map<String, Object>) data;
                disputeSubjectVo.setArea((String) regionData.get("addr"));//区域
                List<Map<String, Object>> dataCounts = (List<Map<String, Object>>) regionData.get("data");

                disputeSubjectVo.setCount(String.valueOf(dataCounts.get(0).get("num")));//案件总数
                disputeSubjectVo.setMan(String.valueOf(dataCounts.get(1).get("num")));//男
                disputeSubjectVo.setWoman(String.valueOf(dataCounts.get(2).get("num")));//女
                list.add(disputeSubjectVo);
            });
        }
        ExcelUtil<DisputeSexVo> excelUtil = new ExcelUtil<DisputeSexVo>(DisputeSexVo.class);
        excelUtil.exportExcel(response, list, "涉事人员性别分析");
    }

    /**
     * 8、时间段高发纠纷类型分析
     */
    @GetMapping("/disputeTime/period")
    public TableDataInfo disputeTimeType(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeTimeVo> list = analysisService.disputeTime(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 8、时间段高发纠纷类型分析导出
     */
    @PostMapping("/disputeTime/export")
    public void disputeTimeExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("时间段高发纠纷类型分析");

        //获取数据
        List<Object> dateResult = statisticsService
                .highTypeStatistics(analysisQueryVo.getDataType()
                        , analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());

        // 动态设置表头
        List<String> headers = new ArrayList<>();
        Map<String, Object> head = (Map<String, Object>) dateResult.get(0);
        headers.add("区域");
        List<Map<String, Object>> data = (List<Map<String, Object>>) head.get("data");
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> map = data.get(i);
            headers.add((String) map.get("type"));
        }

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 填充表头
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        //设置数据
//        for (int i = 0; i < dateResult.size(); i++) {
//            // 这里可以添加数据行
//            Row dataRow = sheet.createRow(i + 1);
//            Map<String, Object> maps = (Map<String, Object>) dateResult.get(i);
//            dataRow.createCell(0).setCellValue((String) maps.get("addr"));
//            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) maps.get("data");
//            for (int j = 0; j < dataAll.size(); j++) {
//                Map<String, Object> map = dataAll.get(j);
//                dataRow.createCell(j + 1).setCellValue(String.valueOf(map.get("num")));
//            }
//        }

        for (int i = 0; i < dateResult.size(); i++) {
            // 这里可以添加数据行
            Row dataRow = sheet.createRow(i + 1);
            Map<String, Object> maps = (Map<String, Object>) dateResult.get(i);
            dataRow.createCell(0).setCellValue((String) maps.get("addr"));
            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) maps.get("data");
            for (int j = 0; j < dataAll.size(); j++) {
                if(j==0){
                    int total=0;
                    Map<String, Object> map = dataAll.get(j);
                    List<Map<String,Object>> nums = (List<Map<String,Object>>)map.get("num");
                    for (int k = 0; k < nums.size(); k++) {
                        StatisticsServiceImpl.TypeRecord typeRecord = (StatisticsServiceImpl.TypeRecord) nums.get(k);
                        total+= typeRecord.getNum();
                    }
                    dataRow.createCell(j + 1).setCellValue(String.valueOf(total));
                }else {
                    String s="";
                    Map<String, Object> map = dataAll.get(j);
                    List<Map<String,Object>> nums = (List<Map<String,Object>>)map.get("num");
                    for (int k = 0; k < nums.size(); k++) {
                        StatisticsServiceImpl.TypeRecord typeRecord = (StatisticsServiceImpl.TypeRecord) nums.get(k);
                        s+= typeRecord.getName();
                    }
                    dataRow.createCell(j + 1).setCellValue(s);
                }
            }
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("时间段高发纠纷类型分析.xlsx")) {
            workbook.write(fileOut);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
            log.info("success!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                log.info("error!");
                e.printStackTrace();
            }
        }
    }

    /**
     * 9、时间段高发纠纷区域分析
     */
    @GetMapping("/disputeTime/area")
    public TableDataInfo disputeTimeArea(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeTimeAreaVo> list = analysisService.disputeTimeArea(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 9、时间段高发纠纷区域分析导出
     */
    @PostMapping("/disputeTimeArea/export")
    public void disputeTimeAreaExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("时间段高发纠纷区域分析");

        //获取数据
        List<Object> dateResult = statisticsService
                .highRegionStatistics(analysisQueryVo.getDataType()
                        , analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());

        // 动态设置表头
        List<String> headers = new ArrayList<>();
        Map<String, Object> head = (Map<String, Object>) dateResult.get(0);
        headers.add("区域");
        List<Map<String, Object>> data = (List<Map<String, Object>>) head.get("data");
        for (int i = 0; i < data.size(); i++) {
            Map<String, Object> map = data.get(i);
            headers.add((String) map.get("type"));
        }

        // 创建表头行
        Row headerRow = sheet.createRow(0);

        // 填充表头
        for (int i = 0; i < headers.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers.get(i));
        }

        //设置数据
        for (int i = 0; i < dateResult.size(); i++) {
            // 这里可以添加数据行
            Row dataRow = sheet.createRow(i + 1);
            Map<String, Object> maps = (Map<String, Object>) dateResult.get(i);
            dataRow.createCell(0).setCellValue((String) maps.get("addr"));
            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) maps.get("data");
            for (int j = 0; j < dataAll.size(); j++) {
                Map<String, Object> map = dataAll.get(j);
                dataRow.createCell(j + 1).setCellValue(String.valueOf(map.get("num")));
            }
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("时间段高发纠纷区域分析.xlsx")) {
            workbook.write(fileOut);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
            log.info("success!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                log.info("error!");
                e.printStackTrace();
            }
        }
    }

    /**
     * 10、时间段高发纠纷纠纷主体分析
     */
    @GetMapping("/disputeTime/subject")
    public AjaxResult disputeTimeSubject(AnalysisQueryVo analysisQueryVo) {
        PageDomain pageDomain = TableSupport.getPageDomain();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        analysisQueryVo.setCurrentPage((pageNum-1)*pageSize);
        analysisQueryVo.setPageSize(pageSize);
        return analysisService.disputeTimeSubject(analysisQueryVo);
    }

    /**
     * 10、时间段高发纠纷纠纷主体分析导出
     */
    @PostMapping("/disputeTimeSubject/export")
    public void disputeTimeSubjectExport(HttpServletResponse response, AnalysisQueryVo analysisQueryVo) {
        if(analysisQueryVo.getSource()==null){
            analysisQueryVo.setSource("");
        }
        //获取导出数据
        List<Object> dateResult = statisticsService
                .highSubjectStatistics(analysisQueryVo.getDataType()
                        , analysisQueryVo.getSource()
                        , analysisQueryVo.getLevel()
                        , analysisQueryVo.getStartTime()
                        , analysisQueryVo.getEndTime()
                        , analysisQueryVo.getId());

        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();
        CellStyle style = workbook.createCellStyle(); // 创建单元格样式
        style.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中

        // 创建一个新的工作表
        Sheet sheet = workbook.createSheet("时间段高发纠纷主体分析");

        Row row = sheet.createRow(0);
        //区域
        Cell cell = row.createCell(0);
        cell.setCellValue("区域");
        cell.setCellStyle(style);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));

        //纠纷总数
        cell = row.createCell(1);
        cell.setCellValue("纠纷总数");
        cell.setCellStyle(style);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));

        Map<String, Object> titleMap = (Map<String, Object>) dateResult.get(0);
        List<Map<String, Object>> titleList = (List<Map<String, Object>>) titleMap.get("data");

        //设置时间标题
        int length = (titleList.size() - 1) / 4;//排除纠纷总数,/4计算总共要遍历几次
        int x = 0;//设置游标
        for (int i = 0; i < length; i++) {
            String type = (String) titleList.get((i + 1) + x).get("type");//获取时间标题
            String title = type.substring(0, 23);//字符串截取时间范围
            cell = row.createCell(2 + x);
            cell.setCellValue(title);
            cell.setCellStyle(style);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 2 + x, 5 + x));//合并单元格
            x += 4;//有4个重复,需要跳跃4个
        }

        //设置总数、自然人、法人组织、非法人组织
        row = sheet.createRow(1);
        x = 0;
        for (int i = 0; i < length; i++) {
            cell = row.createCell(2 + x);//从第二格开始
            cell.setCellValue("总数");
            cell.setCellStyle(style);

            cell = row.createCell(3 + x);
            cell.setCellValue("自然人");
            cell.setCellStyle(style);

            cell = row.createCell(4 + x);
            cell.setCellValue("法人组织");
            cell.setCellStyle(style);

            cell = row.createCell(5 + x);
            cell.setCellValue("非法人组织");
            cell.setCellStyle(style);

            x += 4;
        }

        //导入数据
        for (int i = 0; i < dateResult.size(); i++) {
            //创建第三行
            Row dataRow = sheet.createRow(i + 2);
            Map<String, Object> dataMap = (Map<String, Object>) dateResult.get(i);
            dataRow.createCell(0).setCellValue((String) dataMap.get("addr"));//赋值区域
            dataRow.setRowStyle(style);
            List<Map<String, Object>> dataAll = (List<Map<String, Object>>) dataMap.get("data");
            for (int j = 0; j < dataAll.size(); j++) {
                Map<String, Object> map = dataAll.get(j);
                dataRow.createCell(j + 1).setCellValue(String.valueOf(map.get("num")));
                dataRow.setRowStyle(style);
            }

        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("时间段高发纠纷主体分析.xlsx")) {
            workbook.write(fileOut);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
            log.info("success!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                log.info("error!");
                e.printStackTrace();
            }
        }
    }

    /**
     * 11、调解员工作情况分析 1、总况 2、调解时长 3、调解类型
     * 总况
     */
    @GetMapping("/disputeMediators/all")
    public TableDataInfo disputeMediatorsAll(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeMediatorVo> list = analysisService.disputeMediatorsAll(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 11、调解员工作情况分析 1、总况 2、调解时长 3、调解类型
     * 总况导出
     */
    @PostMapping("/disputeMediatorsAll/export")
    public void disputeMediatorsAllExport(HttpServletResponse response, MediatorVo mediatorVo) {
        //获取数据
        List<MediatorVo> mediatorVos = statisticsService.selectMediatorVoList(mediatorVo);
        ExcelUtil<MediatorVo> excelUtil = new ExcelUtil<MediatorVo>(MediatorVo.class);
        excelUtil.exportExcel(response, mediatorVos, "调解员工作总况分析");
    }

    /**
     * 12、调解员工作情况分析 1、总况 2、调解时长 3、调解类型
     * 时长
     */
    @GetMapping("/disputeMediators/time")
    public TableDataInfo disputeMediatorsTime(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeMediatorTimeVo> list = analysisService.disputeMediatorsTime(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 12、调解员工作情况分析
     * 时长导出
     */
    @PostMapping("/disputeMediatorsTime/export")
    public void disputeMediatorsTimeExport(HttpServletResponse response, MediatorTimeVo mediatorTimeVo) {
        //获取数据
        List<MediatorTimeVo> mediatorVos = statisticsService.selectMediationTimeVoList(mediatorTimeVo);
        ExcelUtil<MediatorTimeVo> excelUtil = new ExcelUtil<MediatorTimeVo>(MediatorTimeVo.class);
        excelUtil.exportExcel(response, mediatorVos, "调解员工作时长分析");
    }

    /**
     * 13、调解员工作情况分析 1、总况 2、调解时长 3、调解类型
     * 调解类型
     */
    @GetMapping("/disputeMediators/type")
    public TableDataInfo disputeMediatorsType(AnalysisQueryVo analysisQueryVo) {
        startPage();
        List<DisputeMediatorTypeVo> list = analysisService.disputeMediatorsType(analysisQueryVo);
        return getDataTable(list);
    }

    /**
     * 13、调解员工作情况分析 1、总况 2、调解时长 3、调解类型
     * 调解类型
     */
    @PostMapping("/disputeMediatorsType/export")
    public void disputeMediatorsTypeExport(HttpServletResponse response, MediationTypeVo mediationTypeVo) {
        //获取数据
        List<MediationTypeVo> mediatorVos = statisticsService.selectMediationTypeVoList(mediationTypeVo);
        ExcelUtil<MediationTypeVo> excelUtil = new ExcelUtil<MediationTypeVo>(MediationTypeVo.class);
        excelUtil.exportExcel(response, mediatorVos, "调解员调解类型分析");
    }

    /**
     * 案件详情列表
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/dispute/list")
    public AjaxResult disputeList(AnalysisQueryVo analysisQueryVo) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        analysisQueryVo.setCurrentPage((pageNum-1)*pageSize);
        analysisQueryVo.setPageSize(pageSize);
        return analysisService.disputeList(analysisQueryVo);
    }
}
